package com.example.lab05.controlller;

import com.example.lab05.entity.Comment;
import com.example.lab05.entity.Member;
import com.example.lab05.entity.Product;
import com.example.lab05.service.CommentService;
import com.example.lab05.service.MemberService;
import com.example.lab05.service.ProductService;
import com.example.lab05.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("comment")
public class CommentController extends BaseController{
    @Autowired
    private  CommentService commentService;

    @GetMapping("query_comment_member_product")
    @ResponseBody
    public List<Comment> queryCommentMemberProduct(){
         return commentService.queryList("query_comment_member_product",null);
    }
    @PostMapping("create")
    @ResponseBody
    public boolean create(@RequestPart MultipartFile[] files, @RequestPart Comment comment){
        Member member = (Member) session.getAttribute("member");
        if (member == null) { // 会员未登录
            return false;
        }
        comment.setMemberId(member.getId());
        Product product = (Product) session.getAttribute("product");
        comment.setProductId(product.getId());

        comment.setIp(request.getRemoteAddr());
        String directory = "d:\\lab\\comment";
        comment.setPictures(UploadUtil.savePicture(files, directory));
        // todo
        commentService.create(comment);

        return true;
    }






}
